package com.ruoyi.train.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.train.domain.StudentInformation;
import com.ruoyi.train.domain.Vo.StudentInformationVo;

import com.ruoyi.train.service.IStudentInformationService;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.multipart.MultipartFile;

/**
 * 导入学生信息Controller
 * 
 * @author wusihao
 * @date 2023-04-13
 */
@RestController
@RequestMapping("/student/importStudentInfo")
public class importStudentInfoController extends BaseController
{
    @Autowired
    private IStudentInformationService studentInformationService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询学员信息列表
     */
    @ApiOperation("查询学生信息")
    @GetMapping("/list/{examApplicationId}")
    public TableDataInfo list(@PathVariable Long examApplicationId)
    {
        startPage();
        List<StudentInformationVo> list = studentInformationService.selectStudentVoByApplicationId(examApplicationId);
        return getDataTable(list);
    }

    /**
     * 导出学员信息列表
     */
    @ApiOperation("导出学生信息")
    @Log(title = "学员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentInformation studentInformation)
    {
        List<StudentInformation> list = studentInformationService.selectStudentInformationList(studentInformation);
        ExcelUtil<StudentInformation> util = new ExcelUtil<StudentInformation>(StudentInformation.class);
        util.exportExcel(response, list, "学员信息数据");
    }

    /**
     * 获取学员信息详细信息
     */
    @ApiOperation("获取学生详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        StudentInformationVo studentInformationVo = studentInformationService.selectStudentInformationById(id);

        return success(studentInformationVo);
    }

    /**
     * 新增学员信息
     */
    @ApiOperation("新增学生信息")
    @Log(title = "学员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentInformation studentInformation)
    {
        return toAjax(studentInformationService.insertStudentInformation(studentInformation));
    }

    /**
     * 修改学员信息
     */
    @ApiOperation("修改学生信息")
    @Log(title = "学员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentInformationVo studentInformationVo)
    {
        return toAjax(studentInformationService.addoperatingScore(studentInformationVo));
    }

    /**
     * 删除学员信息
     */
    @ApiOperation("删除学生信息")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentInformationService.deleteStudentInformationByIds(ids));
    }

    /**
     * 导入数据管理列表
     */
    @Log(title = "学生信息导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<StudentInformationVo> util = new ExcelUtil<StudentInformationVo>(StudentInformationVo.class);
        List<StudentInformationVo> studentInformationList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = studentInformationService.importData(studentInformationList, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StudentInformationVo> util = new ExcelUtil<StudentInformationVo>(StudentInformationVo.class);
        util.importTemplateExcel(response, "学生信息模板");
    }

}
